﻿Imports Mystery
Public Class SayHello
    Implements IContentModificationsSubriver

    Private dict As New Dictionary(Of ComboBoxItem, HelloContent)

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        reload()
        getGlobalObject(Of ContentModifications).subscrive(Me)
    End Sub

    Private Sub reload()
        Dim cd As IContentDispatcher = getGlobalObject(Of IContentDispatcher)()
        Dim greatings As IEnumerable(Of HelloContent) = cd.getAll(Of HelloContent)()
        dict = New Dictionary(Of ComboBoxItem, HelloContent)
        Me.Dispatcher.Invoke(Sub()
                                 cbLanguage.Items.Clear()
                                 For Each c As HelloContent In greatings
                                     Dim cb As New ComboBoxItem() With {.Content = c.language.value}
                                     cbLanguage.Items.Add(cb)
                                     dict(cb) = c
                                 Next
                                 lGreatings.Content = Nothing
                             End Sub)
    End Sub

    Private Sub bRefresh_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles bRefresh.Click
        reload()
    End Sub

    Private _current_greatings As HelloContent

    Private Sub cbLanguage_SelectionChanged(sender As System.Object, e As System.Windows.Controls.SelectionChangedEventArgs) Handles cbLanguage.SelectionChanged
        Dim sel As ComboBoxItem = cbLanguage.SelectedItem
        If sel Is Nothing OrElse Not dict.ContainsKey(sel) Then Exit Sub
        lGreatings.Content = dict(sel).hiThere.value
        _current_greatings = dict(sel)
    End Sub

    Public Sub notificate() Implements Mystery.IContentModificationsSubriver.notificate
        'refresh content
        _current_greatings = getGlobalObject(Of IContentDispatcher).getContent(Of HelloContent)(_current_greatings.guid)
        Me.Dispatcher.Invoke(Sub() lGreatings.Content = _current_greatings.hiThere.value)
    End Sub

    Public ReadOnly Property subscribed As Mystery.IContent Implements Mystery.IContentModificationsSubriver.subscribed
        Get
            Return _current_greatings
        End Get
    End Property
End Class
